package com.point.community;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.demo.SysUserRoleInterface;
import com.alibaba.dubbo.demo.bean.PageBean;
import com.alibaba.dubbo.demo.bean.SysUserRole;
import com.point.community.bean.dwzPageData;

@SuppressWarnings("serial")
public class SysUserRoleAction extends JsonAction {

	Logger logger = Logger.getLogger(SysUserRoleAction.class);
	@Autowired
	private SysUserRoleInterface sysUserRoleService;

	private String msg = "";
	private boolean success = false;
	private String key = "";
	private String obj = "";
	private PageBean pageBean;
	private String statusCode,message,navTabId,rel,callbackType,forwardUrl,confirmMsg;
	
	private List<SysUserRole> sysUserRoleList = new ArrayList<SysUserRole>();

	public SysUserRoleInterface getSysUserRoleService() {
		return sysUserRoleService;
	}

	public void setSysUserRoleService(SysUserRoleInterface sysUserRoleService) {
		this.sysUserRoleService = sysUserRoleService;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getObj() {
		return obj;
	}

	public void setObj(String obj) {
		this.obj = obj;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public String getStatusCode() {
		return statusCode;
	}

	public void setStatusCode(String statusCode) {
		this.statusCode = statusCode;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getNavTabId() {
		return navTabId;
	}

	public void setNavTabId(String navTabId) {
		this.navTabId = navTabId;
	}

	public String getRel() {
		return rel;
	}

	public void setRel(String rel) {
		this.rel = rel;
	}

	public String getCallbackType() {
		return callbackType;
	}

	public void setCallbackType(String callbackType) {
		this.callbackType = callbackType;
	}

	public String getForwardUrl() {
		return forwardUrl;
	}

	public void setForwardUrl(String forwardUrl) {
		this.forwardUrl = forwardUrl;
	}

	public String getConfirmMsg() {
		return confirmMsg;
	}

	public void setConfirmMsg(String confirmMsg) {
		this.confirmMsg = confirmMsg;
	}

	public List<SysUserRole> getSysUserRoleList() {
		return sysUserRoleList;
	}

	public void setSysUserRoleList(List<SysUserRole> sysUserRoleList) {
		this.sysUserRoleList = sysUserRoleList;
	}
	
	@SuppressWarnings("unchecked")
	String webQueryAllSysUserRole()
	{
		String currentPage = request.getParameter("pageNum");
		String numPerPage = request.getParameter("numPerPage");
		if(StringUtils.isEmpty(currentPage)){
			currentPage = "1";
	    }
	    if(StringUtils.isEmpty(numPerPage)){
	    	numPerPage = "20";
	    }
	    int nCurrentPage =  Integer.parseInt(currentPage);    
	    int nNumPerPage =  Integer.parseInt(numPerPage);  
		
		String start = getParameterString("start");
		if (start != null && !start.equals("")) {
			pageBean = new PageBean(start, PAGE_MAX_RECORD_COUNT);
		} else {
			pageBean = new PageBean("0", PAGE_MAX_RECORD_COUNT);
		}
		//参数
		int userid = -1,roleid = -1;
		if (request.getParameterValues("userid") != null)
			userid = getParameterInteger("userid");
		if (request.getParameterValues("roleid") != null)
			roleid = getParameterInteger("roleid");
		String usercode = "",username = "",rolecode = "",rolename = "";
		
		if (request.getParameterValues("usercode") != null)
			usercode = getParameterString("usercode");
		if (request.getParameterValues("username") != null)
			username = getParameterString("username");	

		if (request.getParameterValues("rolecode") != null)
			rolecode = getParameterString("rolecode");
		if (request.getParameterValues("rolename") != null)
			rolename = getParameterString("rolename"); 
		
		pageBean = sysUserRoleService.queryAllSysUserRole(pageBean, userid, usercode, username, roleid, rolecode, rolename);
		
		sysUserRoleList = pageBean.getResultList();

		request.getSession().setAttribute("sysUserRoleList", sysUserRoleList);

		dwzPageData dwz = new dwzPageData();
		dwz.currentPage = Integer.valueOf(nCurrentPage).toString();
		dwz.numPerPage =  Integer.valueOf(nNumPerPage).toString();
		dwz.totalCount = (long) pageBean.getTotal();
		request.getSession().setAttribute("sysRoleFuncDwzData", dwz);
		
		return "sysUserRole";
	}
	
	public String jsonQueryAllSysUserRole()
	{
		logger.error(" jsonQueryAllSysUserRole ");
		//参数
		int userid = -1,roleid = -1;
		if (request.getParameterValues("userid") != null)
			userid = getParameterInteger("userid");
		if (request.getParameterValues("roleid") != null)
			roleid = getParameterInteger("roleid");
		String usercode = "",username = "",rolecode = "",rolename = "";
		
		if (request.getParameterValues("usercode") != null)
			usercode = getParameterString("usercode");
		if (request.getParameterValues("username") != null)
			username = getParameterString("username");	

		if (request.getParameterValues("rolecode") != null)
			rolecode = getParameterString("rolecode");
		if (request.getParameterValues("rolename") != null)
			rolename = getParameterString("rolename");	
		
		sysUserRoleList = sysUserRoleService.queryAllSysUserRoleList(userid, usercode, username, roleid, rolecode, rolename);
		
		boolean ret = sysUserRoleList==null?false:sysUserRoleList.size()>0;
		
		if (ret) 
		{
			success = true;
			msg = "请求成功！";
		} else {
			success = false;
			msg = "请求失败或无数据！";
		}
		return SUCCESS;
	}
	
	public String jsonUpdateSysUserRole() {
		// 参数
		int userid = -1;
		if (request.getParameterValues("userid") != null)
			userid = getParameterInteger("userid");
		String rolelist = "";

		if (request.getParameterValues("rolelist") != null)
			rolelist = getParameterString("rolelist");

		boolean ret = sysUserRoleService.updateSysUserRole(userid, rolelist);

		if (ret) {
			success = true;
			msg = "请求成功！";
		} else {
			success = false;
			msg = "请求失败或无数据！";
		}
		return SUCCESS;
	}
}
